//商机管理//query
Jes.Oppor ={
	init : function(){
		if(!Jes.getUserSess()) return;
		Jes.cBtn('desk.html');
		Jes.Select.oppor_query_dic();
		Jes("search").onclick=function(){Jes.Oppor.query();}
		//Jes("add").onclick=function(){Jes.Oppor.open('op');}
		if(Jes.isFlowAdmin()){
			Jes.show('del');
			//Jes('invalid').onclick=function(){Jes.Oppor.invalid();}
			Jes('del').onclick=function(){Jes.Oppor.del();}
		} 
		initTabs('tabView_1',Array('商机管理'),0,"100%",90,Array(false));
		this.query();
	}
	,query : function(){
		Jes.page("oppor_query_sql",{pageSize:10});
	}
	//我的商机
	,myInit : function(){
		if(!Jes.getUserSess()) return;
		Jes.cBtn('desk.html');
		Jes.sync(function(){Jes.Select.oppor_query_dic();});
		var flow= Jes.request('flow') || '';
		Jes.val('flownode',flow);
		Jes("search").onclick=function(){Jes.Oppor.query();}
		Jes("add").onclick=function(){Jes.Oppor.open('op');}
		Jes("del").onclick=function(){Jes.Oppor.del('del');}
		initTabs('tabView_1',Array('我的商机'),0,"100%",90,Array(false));
		this.query();
	}
	,del : function(){
		var ids = Jes.val("id");
		if(ids == ""){alert("至少选择一项!");return;}
		if(!confirm("你确定删除商机吗？")) return;
		Jes.exec({ids:ids,sql:"oppor_delete_hql"});
	}
	,invalid : function(){
		var ids = Jes.val("id");
		if(ids == ""){alert("至少选择一项!");return;}
		if(!confirm("你确定置商机无效吗？")) return;
		Jes.exec({ids:ids,sql:"oppor_invalid_hql"});
	}
	,open : function(type,id){
		var args= arguments;
		switch(type){
			case "op":
					//Jes.open("../../oppor/html/opporop.html"+(id?"?id="+id:""),300,300);
					//Jes.Dialog.open("商机编辑","../../oppor/html/opporop.html"+(id?"?id="+id:""),300,300);
					//top.Jes.Dialog.open("商机编辑","../../oppor/html/opporop.html"+(id?"?id="+id:""),500,500);
					_newTab(id?'商机编辑':'商机新增',"opporop.html".J({id:id}));
				break;
			case "view":
					var title = args[2];
					_newTab(title,"opporview.html?id="+id+"&name="+title);
				break;
			case "flowinfo":
					_newTab("流程审批信息","flowinfo.html?opporid="+id);
				break;
			case "opporpostop":
					var num=args[3];
					top.tabId = Jes.request("tabId");//临时存放
					top.Jes.Dialog.open(num===undefined?'回复新增':'回复编辑','../../oppor/html/opporpost.html'.J({opporid:id,id:args[2],num:num}),600,400);
				break;
		}
	}
}

//op
Jes.OpporOp = {
	init : function(){
		if(!Jes.getUserSess()) return;
		var id = Jes.request("id");
		Jes("addtj").onclick=function(){Jes.OpporOp.save(1);}//save
		Jes("add").onclick=function(){Jes.OpporOp.save(2);}//save
		Jes.cBtn('myoppor.html');
		if(!id){//insert
			Jes.show('addtj');
			Jes.Select.oppor_dic();
			Jes.vals({"opusername":userInfo.username,"modifyuserid":userInfo.userid});
			Jes.query("roleuser_query_sql",{param:{'rolebh':'flowadmin'}},function(data){
				var html=['<table>'],pro='',deptname='';
				for(var i=0;i<data.length;i++){
					if(i==0) pro ='title="下一环节人员" rule="group" checked onclick="Jes.show(\'_flowtr2\')" rulePre="istj=1"';
					else pro = ' onclick="Jes.show(\'_flowtr2\')"';
					if(deptname != data[i].deptname){
						if(deptname=='') html.push('<tr>');
						else html.push('</td></tr><tr>');
						deptname=data[i].deptname;
						html.push('<td><span style="color:#d4d0c8;">'+deptname+'</span>:</td><td>');
					}
					html.push('<input type="radio" id="userid" name="userid" '+pro+' class="checkbox" value="'+data[i].userid+'">'+data[i].username);
				}
				html.push('<span class="notnull">*</span></td></tr></table>');
				Jes.html('_flowadmins',html);
			});
		}else{
			Jes.get("TBussOppor",{id:id,title:"商机编辑"},function(data){
				if(data.status=='4' || data.status=='5' || (userInfo.userid != data.userid && !Jes.isFlowAdmin())) Jes.read();//complet
				Jes.show('_no');
				Jes.update("TBussOppor",{id:id,clicks:parseInt(data.clicks)+1},null);
				Jes.Select.oppor_dic();
				Jes.vals(data);
				Jes.vals(data,'info');
			});
		}
		
		var fck = new FCKeditor("content","680","300","Basic");
		fck.ReplaceTextarea();
	}
	,view : function(){
		var req = Jes.request(),id=req.id;
		Jes.cBtn('oppor.html');
		Jes("post_top").onclick=function(){Jes.Oppor.open('opporpostop',id);}
		Jes("post_btm").onclick=function(){Jes.Oppor.open('opporpostop',id);}
		//点击数
		Jes.get("TBussOppor",{id:id},function(data){
			Jes.update("TBussOppor",{id:id,clicks:parseInt(data.clicks)+1},null);
			Jes.vals(data);
			//list post
			Jes.OpporPost.query(id);
		});
	}
	,save : function(type){
		if(type ==1){
			//Jes.show('_flowtr1');
			Jes.val('istj',1);//保存提交
			if(!Jes("userid") && Jes.html('_flowadmins').indexOf('notnull')!=-1){alert("下一环节人员没有配置!");return;}
		}else{
			Jes.vals({'istj':0,'flowinfo':''});
		}
		Jes.save("TBussOppor");
	}
}

//商机回复
Jes.OpporPost = {
	postData : null
	,query : function(opporid,isPost,isMove){
		if(isMove) Jes.scrollTo('post_view');
		Jes.html("post_view",'<div style="width:200px;height:36px;text-align:center;"><img src="../../lovejes/images/load/load1.gif" alt="Loading..."/><font color="red">正在读取评论...</font><br></center>');
		var html =[];
		Jes.page('opporpost_query_sql',{param:{opporid:''+opporid+''}},function(data){
			postData= data.pageList,len=postData.length,Jes.OpporPost.postData=postData;
			Jes.html("post_view","");
			for(var i=0;i<len;i++){
				html.push('<div class="post" id="post_div'+i+'"><div>');
				html.push('<div id="post_title" name="post_title"><img src="../themes/default/images/72.png" hspace="7" /><span>'+((data.currentPage-1)*20+i+1)+'楼：</span><span id="post_sub'+i+'">'+postData[i].subject+'</span>['+postData[i].username+']</div>');
				//op
				html.push('<div id="post_op" name="post_op"><table width="80" border="0" cellpadding="0" cellspacing="0">');
				html.push('<tr><td><a href="javascript:Jes.OpporPost.edit('+i+');">修改</a></td><td><a href="javascript:Jes.OpporPost.del('+i+');">删除</a></td></tr></table></div>');
				html.push('<table width="96%" border="0" align="center" cellpadding="0" cellspacing="0">');
				html.push('<tr><td height="25px" class="post_time">发表回复于 '+postData[i].posttime+'</td></tr>');
				html.push('<tr><td class="post_content" id="post_cont'+i+'">'+postData[i].content+'</td></tr></table></div></div>');
			}
			Jes.html("post_view",html);
			var pageH = Jes.pageH(data,"Jes.OpporPost.query('"+opporid+"',false,true)");
			Jes.vals({"page_top1":pageH,"page_btm1":pageH});//分页显示
			if(len>4) Jes.show("page_btm1","post_btm");
			if(isPost) Jes.scrollTo("post_div"+(len-1));
		});
	}
	,del : function(i){
		if(!confirm("你确定删除吗？")) return;
		var param = {ids:this.postData[i].id,opporid:this.postData[i].opporid,sql:"opporpost_delete_hql"};
		//Jes.remove("post_div"+i);
		Jes.exec(param,function(){Jes.remove("post_div"+i);});
	}
	,edit : function(i){
		Jes.oppor.open('opporpostop',this.postData[i].opporid,this.postData[i].id,i);
	}
}

Jes.OpporPostOp={
	init : function(){
		if(!Jes.getUserSess()) return;
		Jes("add").onclick=function(){Jes.OpporPostOp.save();}//save
		Jes.cBtn();
		var req =Jes.request();
		if(!req.id){//insert
			Jes.vals({"subject":"回复","modifyuserid":userInfo.userid,"opporid":req.opporid});
		}else{
			Jes.get("TBussOppost",{id:req.id,title:"回复编辑"});
		}
		Jes.fck("content","470","200","Basic");
	}
	,save : function(){
		var req=Jes.request(),num=req.num;
		Jes.save("TBussOppost",null,function(data){
			var _p = Jes.opener('main','jes_'+top.tabId);
			if(!num){
				_p.Jes.OpporPost.query(req.opporid,true,true);
			}else{
				_p.Jes.html("post_sub"+num,data.subject);
				_p.Jes.html("post_cont"+num,data.content);//动态更新
			}
			Jes.close();//修改不要刷新
		});
	}
}
